﻿using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingForm
{
    public partial class PICCNursingEntryForm : XtraForm
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;   //员工信息
        string Id = string.Empty;
        PatientInfo Patientinfo;    //患者信息
        #endregion

        #region 窗体函数
        public PICCNursingEntryForm(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public PICCNursingEntryForm(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public PICCNursingEntryForm(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        #endregion

        #region 初始化控件数据数据
        private void InitializationControlsData(Control.ControlCollection Controls)
        {
            string sign = txtQianMing.Text;
            foreach (var control in Controls)
            {
                if (control.GetType().Name == "ComboBoxEdit")
                {
                    ((ComboBoxEdit)control).EditValue = "请选择";
                }
                else if (control.GetType().Name == "CheckEdit")
                {
                    ((CheckEdit)control).Checked = false;
                }
                else if (control.GetType().Name == "TextEdit")
                {
                    ((TextEdit)control).Text = "";
                }
                else if (control.GetType().Name == "DateEdit")
                {
                    ((DateEdit)control).DateTime = System.DateTime.Now;
                }
                else if (control.GetType().Name == "TimeEdit")
                {
                    ((TimeEdit)control).Text = System.DateTime.Now.ToString("HH:mm:ss");
                }
                else if (control.GetType().Name == "RichEditControl")
                {
                    ((RichEditControl)control).Text = "";
                }
                else if (control.GetType().Name == "DateTimePicker")
                {
                    ((DateTimePicker)control).Text = "00:00";
                }
                else if (control.GetType().Name == "CheckBox")
                {
                    ((CheckBox)control).Checked = false;
                }
                else if (control.GetType().Name == "MemoEdit")
                {
                    ((MemoEdit)control).Text = "";
                }
            }
            txtQianMing.Text = sign;
        }
        #endregion

        #region 提交
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(txtRiQi.Text.Trim()))
                txtRiQi.DateTime = DateTime.Now;
            //时间判断
            if (!txtShiJian.Text.Contains(":"))
            {
                txtShiJian.Focus();
                MessageBox.Show("时间需HH:mm格式，或使用双击显示当前时间");
                return;
            }

            PICCNursingRecord pnr = new PICCNursingRecord();
            pnr.RiQi = txtRiQi.DateTime.ToString("yyyy-MM-dd");   //日期
            pnr.ShiJian = DateTimeTran.TimeFillZero(txtShiJian.Text);   //时间
            pnr.WaiLuChangDu = textWailu.Text;   //外露长度
            pnr.ShangBiWei = txtBiwei.Text;   //上臂围或腿围
            if (cheGuandao.Checked == true) pnr.GuanDaoTongChang = "√";  //管道是否通畅
            if (cheGansumao.Checked == true) pnr.GengHuanGanSuMao = "√";  //更换肝素帽
            if (cheFutie.Checked == true) pnr.GengHuanFuTie = "√";  //更换敷贴
            pnr.ChongGuanYeTi = txtChongguan.Text;   //冲管液体
            pnr.FengGuan = txtFengguan.Text;   //封管
            pnr.ChuanCiQingKuang = (cheChuanCi1.Checked ? "1" : "0") + "#" + (cheChuanCi2.Checked ? "1" : "0") + "#" + (cheChuanCi3.Checked ? "1" : "0") + "#" + (cheChuanCi4.Checked ? "1" : "0") + "#" + (cheChuanCi5.Checked ? "1" : "0");//穿刺部位情况

            pnr.HuLiCuoShi = memoCuoShi.Text;  //护理措施
            pnr.ShiJian = txtShiJian.Text; //时间
            if (string.IsNullOrEmpty(Id))
                pnr.QianMing = LoginUser.UserName;
            else
                pnr.QianMing = txtQianMing.Text;    //签名
            pnr.FormId = (int)FormType.PICCNursing;     //表单ID
            pnr.FormName = EnumHelper.GetDescription(FormType.PICCNursing);    //表单名字
            record.FormId = (int)FormType.PICCNursing;  //表单ID
            record.FormName = EnumHelper.GetDescription(FormType.PICCNursing);    //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;      //住院号
            record.VisitId = Patientinfo.VISIT_ID;   //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;    //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;    //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;    //当前患者病区名称
            record.AssessmentDate = Convert.ToDateTime(pnr.RiQi + " " + pnr.ShiJian);//日期
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(pnr, Encoding.UTF8);

            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 清空
        private void btn_Empty_Click(object sender, EventArgs e)
        {
            InitializationControlsData(groupControl1.Controls);
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            #region 自动获取数据
            textHuanZheHao.Text = Patientinfo.PATIENT_ID;    //患者号
            textXingMing.Text = Patientinfo.PATIENT_NAME;   //姓名
            textChuangHao.Text = Patientinfo.BED_NO;   //床号
            txtQianMing.Text = LoginUser.UserName;   //护士签名
            #endregion

            txtRiQi.DateTime = DateTime.Now;    //默认显示当前日期
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            PICCNursingRecord pnr = new PICCNursingRecord();
            pnr = XmlUtil.XMLToObject<PICCNursingRecord>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(pnr.RiQi))
                txtRiQi.DateTime = Convert.ToDateTime(pnr.RiQi);   //日期
            textWailu.Text = pnr.WaiLuChangDu;    //外露长度
            txtBiwei.Text = pnr.ShangBiWei;   //上臂围或腿围
            if (pnr.GuanDaoTongChang == "√") cheGuandao.Checked = true;    //管道是否通畅
            if (pnr.GengHuanGanSuMao == "√") cheGansumao.Checked = true;  //更换肝素帽
            if (pnr.GengHuanFuTie == "√") cheFutie.Checked = true;  //更换敷贴

            txtChongguan.Text = pnr.ChongGuanYeTi;    //冲管液体
            txtFengguan.Text = pnr.FengGuan;    //封管
            //穿刺部位情况
            var chaunci = pnr.ChuanCiQingKuang.Split('#');
            if (chaunci[0] == "1")
                cheChuanCi1.Checked = true;
            else
                cheChuanCi1.Checked = false;
            if (chaunci[1] == "1")
                cheChuanCi2.Checked = true;
            else
                cheChuanCi2.Checked = false;
            if (chaunci[2] == "1")
                cheChuanCi3.Checked = true;
            else
                cheChuanCi3.Checked = false;
            if (chaunci[3] == "1")
                cheChuanCi4.Checked = true;
            else
                cheChuanCi4.Checked = false;

            if (chaunci[4] == "1")
                cheChuanCi5.Checked = true;
            else
                cheChuanCi5.Checked = false;


            memoCuoShi.Text = pnr.HuLiCuoShi;   //护理措施
            txtShiJian.Text = DateTimeHelper.HHmmssToHHmm(pnr.ShiJian);     //时间
            txtQianMing.Text = pnr.QianMing;//签名
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 双击显示当前时间
        private void txtShiJian_DoubleClick(object sender, EventArgs e)
        {
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion
    }
}
